package com.um.jdy.platform.base.configuration;

import cn.hutool.core.collection.ListUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.List;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    @Bean
    public Docket docket(@Value("${spring.application.name}") String name, @Value("${spring.profiles.active}") String profile){
        return new Docket(DocumentationType.OAS_30)
                .enable(!profile.equals("prod"))
                .apiInfo(new ApiInfoBuilder().title(name).build())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.ant("/api/v1/**"))
                .build()
                .groupName("代理端")
                .globalRequestParameters(globalRequestParameters());
    }

    @SuppressWarnings("all")
    public List<RequestParameter> globalRequestParameters(){

        return ListUtil.toList(
                new RequestParameterBuilder()
                        .name("token")
                        .description("Token")
                        .in(ParameterType.HEADER)
                        .build(),
                new RequestParameterBuilder()
                        .name("um-session-id")
                        .description("Session ID")
                        .in(ParameterType.HEADER)
                        .build()
        );
    }
    @SuppressWarnings("all")
    public ApiInfo apiInfo(){
        return new ApiInfo(
                "jdy-platform-api-service",
                "",
                "0.0.1",
                "",
                "",
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0" //许可证链接
        );
    }
}